﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.OMS;
using Me.Amon.Util;
using Me.Amon.WMS.C;

namespace Me.Amon.POS
{
    public partial class UpdateCat : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_01)
            {
                Response.Redirect(IEnv.HOME_PAGE);
                return;
            }

            var cat = Request["cat"];
            if (!CharUtil.IsValidateHash(cat))
            {
                cat = "0";
            }

            TbName.MaxLength = CCat.name;

            var dba = new RDBEngine();
            ListCat(dba, user.CorpId, cat);

            dba.Reset();
            ReadCat(dba, user.CorpId, cat);
        }

        #region 导航
        #endregion

        protected void TvCats_SelectedNodeChanged(object sender, EventArgs e)
        {
            var node = TvCats.SelectedNode;
            if (node == null)
            {
                return;
            }

            var dba = new RDBEngine();
            ReadCat(dba, WebUtil.CurrentUser(Session).CorpId, node.Value);
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BnUpdateCat_Click(object sender, EventArgs e)
        {
            SaveCat();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BnDeleteCat_Click(object sender, EventArgs e)
        {
            var id = HdId.Value;
            if (CharUtil.IsValidateCode(id))
            {
                return;
            }

            var dba = new RDBEngine();
            dba.AddTable(DCat.TABLE);
            dba.AddWhere(DCat.ID, id);
            dba.ExecuteDelete();
        }

        protected void BnCancelCat_Click(object sender, EventArgs e)
        {
            var node = TvCats.SelectedNode;
            if (node == null)
            {
                return;
            }

            Response.Redirect("~/POS/Spus.aspx?cat=" + node.Value);
        }

        #region 目录
        private void ListCat(RDBEngine dba, string corp, string cat)
        {
            var root = new TreeNode();
            root.Value = "0";
            root.Text = "商品类别";
            TvCats.Nodes.Add(root);
            root.Selected = (cat == root.Value);

            dba.AddTable(DCat.TABLE);
            dba.AddColumn(DCat.ID);
            dba.AddColumn(DCat.NAME);
            dba.AddWhere(DCat.CORP_ID, corp);
            dba.AddWhere(DCat.PID, "0");
            dba.AddWhere(DCat.STATUS, CCat.STATUE_ENABLED.ToString(), false, false);

            foreach (DataRow row in dba.ExecuteSelect().Rows)
            {
                var node = new TreeNode();
                node.Value = "" + row[DCat.ID];
                node.Text = "" + row[DCat.NAME];
                root.ChildNodes.Add(node);
                node.Selected = (cat == node.Value);

                LoadCat(dba, node, corp, cat);
            }
        }

        private void LoadCat(RDBEngine dba, TreeNode root, string corp, string cat)
        {
            dba.Reset();
            dba.AddTable(DCat.TABLE);
            dba.AddColumn(DCat.ID);
            dba.AddColumn(DCat.NAME);
            dba.AddWhere(DCat.CORP_ID, corp);
            dba.AddWhere(DCat.PID, root.Value);
            dba.AddWhere(DCat.STATUS, CCat.STATUE_ENABLED.ToString(), false, false);

            foreach (DataRow row in dba.ExecuteSelect().Rows)
            {
                var node = new TreeNode();
                node.Value = "" + row[DCat.ID];
                node.Text = "" + row[DCat.NAME];
                root.ChildNodes.Add(node);
                node.Selected = (cat == node.Value);

                LoadCat(dba, node, corp, cat);
            }
        }
        #endregion

        private void ReadCat(RDBEngine dba, string corp, string cat)
        {
            dba.AddTable(DCat.TABLE);
            dba.AddWhere(DCat.CORP_ID, corp);
            dba.AddWhere(DCat.ID, cat);
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 1)
            {
                return;
            }

            var row = dt.Rows[0];
            HdId.Value = "" + row[DCat.ID];
            LlCode.Text = "" + row[DCat.CODE];
            TbName.Text = "" + row[DCat.NAME];
            CbStatus.Checked = false;
        }

        public void SaveCat()
        {
            LlErr.Text = "";

            var node = TvCats.SelectedNode;
            if (node == null)
            {
                LlErr.Text = "请选择上级类别！";
                return;
            }

            var name = TbName.Text.Trim();
            if (name.Length >= CCat.name)
            {
                LlErr.Text = string.Format("类别长度不能超过{0}个字符！", CCat.name);
                return;
            }

            var id = HdId.Value;
            var user = WebUtil.CurrentUser(Session);

            var dba = new RDBEngine();
            dba.AddTable(DCat.TABLE);
            dba.AddParam(DCat.CORP_ID, user.CorpId);
            dba.AddParam(DCat.NAME, name);
            dba.AddParam(DCat.STATUS, CbStatus.Checked ? CCat.STATUS_DISABLED : CCat.STATUE_ENABLED);
            dba.AddParam(DCat.UPDATE_TIME, DCat.NOW, false);
            dba.AddParam(DCat.UPDATE_USER, user.UserId);
            if (CharUtil.IsValidateHash(id))
            {
                // 更新操作
                dba.AddWhere(DCat.ID, id);
                dba.ExecuteUpdate();
                node.Text = name;
            }
            else
            {
                // 新增操作
                id = HashUtil.UtcTimeInHex();
                dba.AddParam(DCat.ID, id);
                dba.AddParam(DCat.CODE, "");
                dba.AddParam(DCat.PID, node.Value);
                dba.AddParam(DCat.TID, "0");
                dba.AddParam(DCat.CREATE_TIME, DCat.NOW, false);
                dba.AddParam(DCat.CREATE_USER, user.UserId);
                dba.ExecuteInsert();

                var temp = new TreeNode();
                temp.Value = id;
                temp.Text = name;
                node.ChildNodes.Add(temp);
            }

            ShowDef();
        }

        public void ShowDef()
        {
            HdId.Value = "";
            LlCode.Text = "";
            TbName.Text = "";
            CbStatus.Checked = false;
        }
    }
}